TEST(1V) — UNIX Programmer’s Manual
NAME
test − perform tests and return status (sh only)
SYNOPSIS
test expression
[ expression ]
DESCRIPTION
Test evaluates the expression expr, and if its value is true then returns zero exit status; otherwise, a non zero exit status is returned. Test also returns a non zero exit if there are no arguments.
The following primitives are used to construct expr.
−r file true if the file exists and is readable.
−w file true if the file exists and is writable.
−x file true if the file exists and is executable.
−f file true if the file exists and is a regular file.
−d file true if the file exists exists and is a directory.
−c file true if the file exists and is a character device.
−b file true if the file exists and is a block device.
−p file true if the file exists and is a named pipe (see mkfifo(1v)).
−u file true if the file exists and the setuid bit is set (bit 04000 in the file mode bits).
−g file true if the file exists and the setgid bit is set (bit 02000 in the file mode bits).
−s file true if the file exists and has a size greater than zero.
−t [fildes] true if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device.
−z s1 true if the length of string s1 is zero.
−n s1 true if the length of the string s1 is nonzero.
s1 = s2 true if the strings s1 and s2 are equal.
s1 != s2 true if the strings s1 and s2 are not equal.
s1 true if s1 is not the null string.
n1 −eq n2
true if the integers n1 and n2 are algebraically equal. Any of the comparisons −ne, −gt, −ge, −lt, or −le may be used in place of −eq.
These primaries may be combined with the following operators:
! unary negation operator.
−a binary and operator.
−o binary or operator.
( expr ) parentheses for grouping.
−a has higher precedence than −o. Notice that all the operators and flags are separate arguments to test. Notice also that parentheses are meaningful to the Shell and must be escaped.
When using the command [ the command must be followed by a space and the final ] must be given and preceded by a space.
NOTES
The command is a shell built-in. Programs with identical names exist but have slightly different semantics − see test(1).
SEE ALSO
System V